<template>
  <div>
    <!-- 按钮 -->
    <!-- <el-button @click="delete1">删除</el-button> -->
    <el-button type="primary" icon="el-icon-plus" style="margin:10px 6px" @click="showDialog">添加</el-button>
    <!-- 表格组件
      data 数据
      border 边框
      column属性
      lable 显示标题
      width 对应列的宽度
      align 对齐方式
     -->
    <el-table border :data="list" style="width: 100%">
      <!-- column列 -->
      <el-table-column type="index" prop="prop" label="序号" width="80px" align="center" />
      <el-table-column prop="tmName" label="品牌名称" width="width" />
      <el-table-column prop="logoUrl" label="品牌logo" width="width">
        <template slot-scope="{row}">
          <img :src="row.logoUrl" style="width:40px;height:40px">
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{row}">
          <el-button type="warning" icon="el-icon-edit" size="mini" @click="updataTradeMark(row)">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deletFm(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器
        当前第几页
        数据总条数
        每一页展示数据条数
        连续页数
     -->
    <el-pagination style="margin-top:20px;text-align:center" :pager-count="7" :page-sizes="[3,5,10]" :current-page="page" :total="total" :page-size="limit" layout="prev, pager, next,jumper,->,sizes,total" @size-change="handlersize" @current-change="getPageList" />

    <!-- 对话框
    :visible.sync 控制对话框显示隐藏
     -->
    <el-dialog :title="tmForm.id?'修改品牌':'添加品牌'" :visible.sync="dialogFormVisible">
      <el-form ref="ruleForm" :rules="rules" :model="tmForm" style="width:80%">
        <el-form-item prop="tmName" label="品牌名称" label-width="100px">
          <el-input v-model="tmForm.tmName" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="logoUrl" label="品牌图片" label-width="100px">
          <el-upload action="/dev-api/admin/product/fileUpload" class="avatar-uploader" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="updateOrAddTrademark">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'TradeMark',
  data() {
    return {
      page: 1,
      limit: 5,
      list: [],
      total: 0,

      // 对话框显示与隐藏
      dialogFormVisible: false,
      form: {},

      // 新增品牌
      tmForm: {
        id: '',
        tmName: '',
        logoUrl: ''
      },
      // 表单验证规则
      rules: {
        tmName: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3至10个字符之间', trigger: 'change' }
        ],
        logoUrl: [{ required: true, message: '请选择图片' }]
      }
    }
  },
  mounted() {
    // 获取列表数据的方法
    this.getPageList()
  },
  methods: {
    // 获取列表数据的方法
    getPageList(pager = 1) {
      this.page = pager
      const { page, limit } = this
      return this.$API.tradeMark.reqTradeMarkList(page, limit).then(res => {
        // eslint-disable-next-line eqeqeq
        if (res.code == 200) {
          this.total = res.data.total
          this.list = res.data.records
        }
      })
    },
    // 当分页器某一页展示数据条数发生变化时触发
    handlersize(limit) {
      this.limit = limit
      this.getPageList()
    },
    // 点击显示dialog
    showDialog() {
      this.dialogFormVisible = true
      this.tmForm = {
        id: '',
        tmName: '',
        logoUrl: ''
      }
    },
    // 修改品牌
    updataTradeMark(row) {
      // 浅拷贝
      this.tmForm = { ...row }
      this.dialogFormVisible = true
    },
    // 上传图片成功
    handleAvatarSuccess(res) {
      this.tmForm.logoUrl = res.data
    },
    // 上传图片之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 取消按钮
    cancel() {
      this.dialogFormVisible = false
      this.tmForm = {
        id: '',
        tmName: '',
        logoUrl: ''
      }
    },
    // 添加或修改品牌
    updateOrAddTrademark() {
      // 所有验证都完事再去提交数据
      this.$refs.ruleForm.validate(async success => {
        // 发送请求(添加品牌|修改品牌)
        if (success) {
          const res = await this.$API.tradeMark.reqSaveOrUpdateTrademark(this.tmForm)
          if (res.code === 200) {
            // 弹出信息提示框:添加成功,修改成功
            this.$message({
              type: 'success',
              message: this.tmForm.id ? '修改成功' : '添加成功'
            })
            // 操作成功后,需要再次获取列表展示
            // 如果添加品牌停留在第一页,如果修改品牌,停留在当前页
            this.getPageList(this.tmForm.id ? this.page : 1)
          }
          this.dialogFormVisible = false
        }
      })
    },
    // 删除品牌
    deletFm(row) {
      this.$confirm(`你确定删除${row.tmName}吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$API.tradeMark.reqDeleteTradeMark(row.id)
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getPageList(this.page)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
